﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TicTacToe
{
    public class Game
    {
        public bool checkwinner(char[,] m, char C)
        {
            bool winner = false;
            if (((m[0, 0] == C) && (m[0, 1] == C) && (m[0, 2] == C)) ||
                 ((m[1, 0] == C) && (m[1, 1] == C) && (m[1, 2] == C)) ||
                 ((m[2, 0] == C) && (m[2, 1] == C) && (m[2, 2] == C)) ||
                 ((m[0, 0] == C) && (m[1, 0] == C) && (m[2, 0] == C)) ||
                 ((m[0, 1] == C) && (m[1, 1] == C) && (m[2, 1] == C)) ||
                 ((m[0, 2] == C) && (m[1, 2] == C) && (m[2, 2] == C)) ||
                 ((m[0, 0] == C) && (m[1, 1] == C) && (m[2, 2] == C)) ||
                 ((m[0, 2] == C) && (m[1, 1] == C) && (m[2, 0] == C)))
                winner = true;
            return winner;
        }

        public void RunGame()
        {

            int numberofplay = 0;
            bool winner = false;
            bool quit = false;
            Draw d = new Draw();
            Player p = new Player();
            Game g = new Game();
            char[,] mylla = new char[3, 3];
            AI a = new AI();

            Console.WriteLine("Welcome to TicTacToe game, version: "+ System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());

            while ((!winner) && (numberofplay < 9) && (!quit))
            {
                // 1. Teikna borð
                // 2. Leikmaður gerir. Input með verify etc ,numplay++
                // 3. Check if winner
                // 4. Tölva gerir, numplay++
                // 5. check if winner 

                d.DrawTicTacToeTable(mylla);

                // human player

                if (p.Player_1(mylla))
                {
                    if (g.checkwinner(mylla, 'X'))
                    {
                        d.DrawTicTacToeTable(mylla);
                        Console.WriteLine("You are the winner ");
                        winner = true;
                    }
                    numberofplay++;
                }
                else
                    quit = true;

                // computer player
                if ((!quit) && (!winner))
                    if (a.play(mylla))
                    {
                        if (g.checkwinner(mylla, 'O'))
                        {
                            d.DrawTicTacToeTable(mylla);
                            Console.WriteLine("Computer won ");
                            winner = true;
                        }
                        numberofplay++;
                    }

            } //end while no winner    
        }
    }
}
